//
//  StatusCell.swift
//  JJWeibo
//
//  Created by JJ on 15/9/6.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit

/// 默认的图片大小
let JJStatusPictureItemWidth: CGFloat = 90
/// 默认的图片间距
let JJStatusPictureItemMargin: CGFloat = 10
/// 每行的最大图片数量
let JJStatusPictureMaxCount: CGFloat = 3
/// 配图视图的最大的尺寸
let JJStatusPictureMaxWidth = JJStatusPictureMaxCount * JJStatusPictureItemWidth + (JJStatusPictureMaxCount - 1) * JJStatusPictureItemMargin

class StatusCell: UITableViewCell {
    
    func rowHeight(viewModel:StatusViewModel)->CGFloat{
        
        statusViewModel=viewModel
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
     
        setUI()
    }
  
    var statusViewModel:StatusViewModel?{
        didSet{
            topView.statusViewModel=statusViewModel
            let conText=statusViewModel?.status.text ?? ""
            contenLable.attributedText = EmoticonViewModel.shareViewModel.emoticonText(conText, font: contenLable.font)
            pictureView.statusViewModel=statusViewModel
            
            pictureViewheightCons?.constant=pictureView.bounds.height
            pictureViewWidthCons?.constant=pictureView.bounds.width
            pictureViewTopCons?.constant=statusViewModel?.thumbnailURLs?.count==0 ? 0 : JJStatusCellMargin
        }
        
    }

    var pictureViewWidthCons:NSLayoutConstraint?
    var pictureViewheightCons:NSLayoutConstraint?
    var pictureViewTopCons:NSLayoutConstraint?
    
    func setUI(){
        
        backgroundColor=UIColor.whiteColor()
        let topSepView=UIView()
        topSepView.backgroundColor=UIColor(white: 0.85, alpha: 1.0)
    
        contentView.addSubview(topSepView)
        contentView.addSubview(topView)
        contentView.addSubview(contenLable)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
    
        let width = UIScreen.mainScreen().bounds.width
        topSepView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
       topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: JJStatusCellMargin + JJStatusIconWidth), offset:CGPoint(x: 0, y: 10))
        contenLable.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: JJStatusCellMargin, y: JJStatusCellMargin))
    
 
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView:pictureView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -JJStatusCellMargin, y: JJStatusCellMargin))
//        bottomView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: contentView, size: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var topView:StatusCellTopView=StatusCellTopView()
     lazy var contenLable=UILabel(title:nil ,color: UIColor.darkGrayColor(),fontsize: 15,layoutWidth:UIScreen.mainScreen().bounds.width-2 * JJStatusCellMargin)
    
     lazy var pictureView:StatusPictureView=StatusPictureView()
     lazy var bottomView:StatusCellBottomView=StatusCellBottomView()
    
    
}
